import shapefile


r = shapefile.Reader("shapefiles/NYC_MUSEUMS/NYC_MUSEUMS_GEO")
# 1. pyshp 在读取的时候不关心文件的扩张命
#    此处可以为 "NYC_MUSEUMS_GEO.shp" 或 "NYC_MUSEUMS_GEO.shx"
#
# 2. 也可与上下文管理器连用
#       with shapefile.Reader("NYC_MUSEUMS_GEO.shp") as r:
#           print(r)
#
# 3. 从类似文件的对象中读取 Shapefile
#       shp = open("NYC_MUSEUMS_GEO.shp", "rb")
#       dbf = open("NYC_MUSEUMS_GEO.dbf", "rb")
#       r = shapefile.Reader(shp=shp, dbf=dbf)

print(r)
# 读取shape文件信息

print(r.shapeType)
# 读取数据类型
# NULL = 0；POINT = 1；POLYLINE = 3；POLYGON = 5；MULTIPOINT = 8；POINTZ = 11；POLYLINEZ = 13
# POLYGONZ = 15；MULTIPOINTZ = 18；POINTM = 21；POLYLINEM = 23；POLYGONM = 25；MULTIPOINTM = 28
# MULTIPATCH = 31
# 例子中返回值为1，说明shp文件为POINT文件

# 检查文件中包含的数据数量
print(len(r))

# 边界框区域
print(r.bbox)

print(r.shapes()[0])
for i in r.shapes():
    print(i.patrs)

# 读取图形的坐标
print(r.shapes()[0].points)
#  如多边形，该图形由多个点按照顺序连接而成，第一个点与最后一个点坐标相同
#  [(480917.70, 4477899.80), (480937.09, 4479096.66),...(480917.70, 4477899.80)]

# 读取记录
record = r.record()

# 读取字段（属性字段）
for field in r.fields:
    print(field)
#  图形由多个字段组成，每个元祖为一个字段，'ID' 为字段名称，'N' 字段类型，8 为长度，0 为（书上有，以后补充）
#  [('DeletionFlag', 'C', 1, 0), ['ID', 'N', 8, 0], ['面积', 'N', 15, 6], ['周长', 'N', 15, 6]...]
#
# 关闭
r.close()

